"""
    @Author  ：思念 
    @File    ：2.CSV写入-字典.py
    @Date    ：2024/12/9 21:54 
"""
import csv

rows = [
    {
        "class_name": "18级Python",
        "name": '小王',
        "gender": '男',
        "phone": '13146060xx1',
        "qq": '123456xx1'
    },
    {
        "class_name": "18级Python",
        "name": '小李',
        "gender": '男',
        "phone": '13146060xx2',
        "qq": '123456xx2'
    },
    {
        "class_name": "19级Python",
        "name": '小赵',
        "gender": '女',
        "phone": '13146060xx3',
        "qq": '123456xx3'
    },
    {
        "class_name": "19级Python",
        "name": '小红',
        "gender": '女',
        "phone": '13146060xx4',
        "qq": '123456xx4'
    },
]

headers = list(rows[0].keys())

with open('../字典写入.csv', 'w', encoding='gbk', newline='') as f:
    # 创建一个csv的DictWriter对象，这样才能够将写入csv格式数据到这个文件
    f_csv = csv.DictWriter(f, headers)
    # 写入一行（我们用第一行当做表头）
    f_csv.writeheader()
    # 写入多行行（当做数据）
    f_csv.writerows(rows)
